package com.iecas.runnner;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.log.StaticLog;
import com.iecas.enums.BizEnum;
import com.iecas.launch.Client;
import com.iecas.request.HeartBeatReq;
import com.iecas.request.Root;
import com.iecas.utils.RequestUtils;
import lombok.Data;

import java.nio.ByteBuffer;
import java.util.concurrent.TimeUnit;

@Data
public class HeartBeatRunner extends Thread{
    Client client;
    @Override
    public void run() {
        while (true){
            if (Thread.currentThread().isInterrupted()) {
                break;
            }
            // 发生心跳
            HeartBeatReq heartBeatReq = new HeartBeatReq();
            heartBeatReq.setCode(BizEnum.HEART_BEAT_REQ.getCode());
            ByteBuffer encode = RequestUtils.encode(heartBeatReq);
            client.write(encode);
            StaticLog.info("发送心跳包...");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                interrupt();
            }
        }
    }

    public void shutdown(){
        interrupt();
    }

    public void setClient(Client client){
        this.client = client;
    }
}
